import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/portal",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login"),
    meta: {
      title: "登录",
      keepAlive: false,
    },
  },
  {
    path: "/portal",
    name: "portal",
    component: () => import("@/views/portal"),
    meta: {
      title: "首页",
      keepAlive: false,
    },
  },
  {
    path: "/goods-details/:gId",
    name: "goodsDetails",
    component: () => import("@/views/goods-details"),
  },
  {
    path: "/cart",
    name: "shoppingCart",
    component: () => import("@/views/shopping-cart"),
    meta: {
      title: "购物车",
      keepAlive: false,
    },
  },
];

const router = new VueRouter({
  routes,
});

// 导航守卫
router.beforeEach((to, form, next) => {
  // 设置页面的标题
  const title = to.meta.title;
  if (title) {
    document.title = title;
  }
  // 放行
  next();
});

export default router;
